/*-----------------------------------------------------------------------------------
 *
 * 프로그램명	: CommonDAO.java
 * Version		: 1.0
 * 작성일 		: 2006-08-16
 * 작성자		:
 * 수정일 		:
 * 수정자		:
 * 설명		 	:
 * 기능		 	:
 *
 * Copyright (c) 2000 by H&H  All Rights Reserved.
 *
-------------------------------------------------------------------------------------*/
package com.zll.framework.model;

import java.io.*;



import java.util.*;

import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.CellRangeAddress;
import org.apache.poi.hssf.util.HSSFColor;
//import org.apache.poi.ss.usermodel.Font;

import com.zll.framework.db.*;
import com.zll.framework.util.*;

public class HashList extends ArrayList
{
	private HashMap metaMap = new HashMap();
	private HashMap metaGroupMap = new HashMap();
	private ArrayList headerList = new ArrayList();			//엑셀다운로드시 엑셀 헤더 목록
	private ArrayList headerGroupList = new ArrayList();	//엑셀다운로드시 엑셀 헤더그룹 목록
	private ArrayList searchList = new ArrayList();			//엑셀다운로드시 검색조건 목록

	//특정 칼럼의 합을 구한다.
	public String getSum(String columnName) {
		String sum = "";
//		if ("INTEGER".equals(this.getColumnType(columnName))) {
//			int iSum = 0;
//			for (int i = 0; i < this.size(); i++) {
//				iSum += Integer.parseInt(this.get(columnName,i));
//			}
//			sum = String.valueOf(iSum);
//		}
//		else if ("DECIMAL".equals(this.getColumnType(columnName))) {
//
//   			BigDecimal dSum = new BigDecimal("0.0");
//   			for (int i = 0; i < this.size(); i++) {
//   				dSum = dSum.add(new BigDecimal(this.get(columnName,i)));
//   			}
//
//			sum = dSum.toString();
//		}
		return sum;
	}

	public void setColumn(String columnName, int columnType, int precision, int scale, int isNullable)
	{
		//System.out.println("columnName:" + columnName + "  columnType:"+ columnType + " precision:" + precision +
		//" scale:" + scale + " isNullable:" + isNullable);
		metaMap.put(columnName, new MetaData(columnName, columnType, precision, scale, isNullable));
	}
	
	public void setSearch(String searchName, String searchValue) {
		searchList.add(new MetaData(searchName, searchValue));
	}
	
	public void setHeader(String columnName, String header, int align) {
		MetaData md = (MetaData)metaMap.get(columnName);
		if (md != null) {
			md.columnHeader = header;
			md.align = align;
			headerList.add(md);
		}
	}
	
	public void setHeaderGroup(String columnName, String header, int rowspan, int colspan) {
		headerGroupList.add(new MetaData(columnName, header, rowspan, colspan));
	}

	public boolean existsColumn(String columnName) {
		return metaMap.containsKey(columnName);
	}

	public int getColumnType(String columnName) {
		MetaData md = (MetaData)metaMap.get(columnName);
		//System.out.println("============>columnName:" + columnName + "  md:" + md);
		return md.columnType;
	}

	public int getPrecision(String columnName) {
		MetaData md = (MetaData)metaMap.get(columnName);
		return md.precision;
	}

	public int getScale(String columnName) {
		MetaData md = (MetaData)metaMap.get(columnName);
		return md.scale;
	}

	public boolean getNecessary(String columnName) {
		MetaData md = (MetaData)metaMap.get(columnName);
		return md.necessary;
	}

	public String get(String key, int index) {
		if (this.size() == 0 || index >= this.size()) {
			return "";
		} else {
			HashMap map = (HashMap)this.get(index);
			return (String)map.get(key);
		}
	}

	public int getInt(String key, int index) {
		if (this.size() == 0 || index >= this.size()) {
			return 0;
		} else {
			HashMap map = (HashMap)this.get(index);
			String val = (String)map.get(key);
			if (val == null || val.equals("")) {
				return 0;
			} else {
				return Integer.parseInt(val);
			}
		}
	}

	public String getToBr(String key, int index) {
		if (this.size() == 0 || index >= this.size()) {
			return "";
		} else {
			HashMap map = (HashMap)this.get(index);
			String val = StringUtil.nvl(map.get(key));
			return val.replaceAll("\n", "<br/>");
		}
	}
	
	public String getDate(String key, int index) {
		return getDate(key, index, "-");
	}
	
	public String getDate(String key, int index, String delimeter) {
		if (this.size() == 0 || index >= this.size()) {
			return "";
		} else {
			HashMap map = (HashMap)this.get(index);
			return StringUtil.toDate((String)map.get(key), delimeter);
		}
	}

	public String getPrice(String key, int index) {
		if (this.size() == 0 || index >= this.size()) {
			return "";
		} else {
			HashMap map = (HashMap)this.get(index);
			return StringUtil.toPrice((String)map.get(key));
		}
	}
	
	public String getEncode(String key, int index) throws Exception {
		if (this.size() == 0 || index >= this.size()) {
			return "";
		} else {
			HashMap map = (HashMap)this.get(index);
			return java.net.URLEncoder.encode((String)map.get(key), "utf-8");
		}
	}

	public void set(String key, int index, String value) {
		if (this.size() == 0 || index >= this.size()) {
			//
		} else {
			HashMap map = (HashMap)this.get(index);
			map.put(key, value);
		}
	}

	public void add(HashList addList) {
		for (int i = 0; i < addList.size(); i++) {
			this.add((HashMap)addList.get(i));
		}
	}

	public void put(String key, int index, String value) {
		if (this.size() == 0 || index >= this.size()) {
			HashMap map = new HashMap();
			map.put(key, value);
			this.add(map);
		} else {
			HashMap map = (HashMap)this.get(index);
			map.put(key, value);
		}
	}

	public void writeToExcel(HttpServletResponse response, String title) throws Exception {
		
		//response.setHeader("Content-Disposition","attachment;filename="+java.net.URLEncoder.encode(title, "utf-8")+"_"+StringUtil.today()+".xls");
//		response.setHeader("Content-Disposition","attachment;filename="+title+"_"+StringUtil.today()+".xls");
//		response.setHeader("Content-Description","JSP Generated Data");
	
		HSSFWorkbook wb = null;
		OutputStream fileOut = null;

		try {
			int rowCount = 0;
			wb = new HSSFWorkbook();
			HSSFSheet sheet = wb.createSheet(title);
			HSSFCell cell = null;
			
			// 셀스타일
			HSSFCellStyle titleStyle 		= ExcelUtil.getCellStyle(wb, 18, HSSFFont.BOLDWEIGHT_BOLD, HSSFCellStyle.ALIGN_LEFT, false, false, false); 
			HSSFCellStyle searchStyle 		= ExcelUtil.getCellStyle(wb, 10, HSSFFont.BOLDWEIGHT_BOLD, HSSFCellStyle.ALIGN_RIGHT, false, false, false); 
			HSSFCellStyle searchValueStype	= ExcelUtil.getCellStyle(wb, 10, HSSFFont.BOLDWEIGHT_NORMAL, HSSFCellStyle.ALIGN_LEFT, false, false, false);
			HSSFCellStyle headerStyle 		= ExcelUtil.getCellStyle(wb, 10, HSSFFont.BOLDWEIGHT_NORMAL, HSSFCellStyle.ALIGN_CENTER, true, true, false); 
			HSSFCellStyle style	 			= ExcelUtil.getCellStyle(wb, 10, HSSFFont.BOLDWEIGHT_NORMAL, HSSFCellStyle.ALIGN_LEFT, true, false, false);
			HSSFCellStyle style2	 		= ExcelUtil.getCellStyle(wb, 10, HSSFFont.BOLDWEIGHT_NORMAL, HSSFCellStyle.ALIGN_CENTER, true, false, false);
			HSSFCellStyle numberStyle		= ExcelUtil.getCellStyle(wb, 10, HSSFFont.BOLDWEIGHT_NORMAL, HSSFCellStyle.ALIGN_RIGHT, true, false, true);
		
			
			//제목
			HSSFRow row = sheet.createRow(rowCount++);
			row.setHeightInPoints(30);
			cell = row.createCell(0);
			cell.setCellValue(new HSSFRichTextString(title));
			cell.setCellStyle(titleStyle);
			sheet.addMergedRegion(new CellRangeAddress(0, (short)0, 0, (short)2));


			
			
			//검색조건
			row = sheet.createRow(rowCount++);
			row.setHeightInPoints(5);
			
			for (int i = 0 ; i < searchList.size(); i++) {
				row = sheet.createRow(rowCount++);
				row.setHeightInPoints(20);
				
				for (int j = 0; j < headerList.size()-2; j++) {
					cell = row.createCell(0);
				}
				
				cell = row.createCell(headerList.size()-2);
				cell.setCellValue(new HSSFRichTextString(((MetaData)searchList.get(i)).searchName));
				cell.setCellStyle(searchStyle);
				
				cell = row.createCell(headerList.size()-1);
				cell.setCellValue(new HSSFRichTextString(((MetaData)searchList.get(i)).searchValue));
				cell.setCellStyle(searchValueStype);
			}
			
			if (searchList.size() > 0) {
				row = sheet.createRow(rowCount++);
				row.setHeightInPoints(5);
			}
	
			
			// 헤더그룹
			if (headerGroupList.size() > 0) {
				row = sheet.createRow(rowCount++);
				row.setHeightInPoints(20);
				int startCol = 0;
				int lastCol = 0;
				int cellPos = 0;
				for (int i = 0; i < headerGroupList.size(); i++) {
					MetaData md = (MetaData)headerGroupList.get(i);
					cell = row.createCell(cellPos++);
					cell.setCellValue(new HSSFRichTextString(md.columnHeader));
					cell.setCellStyle(headerStyle);
					
					for (int j = 0; j < md.colspan-1; j++) {
						cell = row.createCell(cellPos++);
						cell.setCellValue(new HSSFRichTextString(""));
						cell.setCellStyle(headerStyle);
					}
					
					lastCol = (startCol + md.colspan-1);
					sheet.addMergedRegion(new CellRangeAddress(rowCount-1, rowCount-1+md.rowspan-1, startCol, lastCol));
					
					startCol = lastCol+1;
				}
			}
			
			// 헤더
			row = sheet.createRow(rowCount++);
			row.setHeightInPoints(20);
			for (int i = 0; i < headerList.size(); i++) {
				MetaData md = (MetaData)headerList.get(i);
				cell = row.createCell(i);
				cell.setCellValue(new HSSFRichTextString(md.columnHeader));
				cell.setCellStyle(headerStyle);
			}

		
			// 데이타
			for (int i = 0; i < this.size(); i++) {
				
				row = sheet.createRow(rowCount++);
				row.setHeightInPoints(20);
				
				for (int j = 0; j < headerList.size(); j++) {
					MetaData md = (MetaData)headerList.get(j);
					cell = row.createCell(j);
					if (md.align == MetaData.ALIGN_CENTER) {
						cell.setCellValue(new HSSFRichTextString(this.get(md.columnName,i)));
						cell.setCellStyle(style2);
						cell.setCellType(HSSFCell.CELL_TYPE_STRING);
					} else if (md.columnType == MetaData.TYPE_DECIMAL || md.columnType == MetaData.TYPE_INTEGER) {
						cell.setCellValue(this.getInt(md.columnName,i));
						cell.setCellStyle(numberStyle);
					} else {
						cell.setCellValue(new HSSFRichTextString(this.get(md.columnName,i)));
						cell.setCellStyle(style);
						cell.setCellType(HSSFCell.CELL_TYPE_STRING);
					}
				}
			}
			
			for (int i = 0; i < headerList.size(); i++) {
				sheet.autoSizeColumn((short)i);
				sheet.setColumnWidth(i, sheet.getColumnWidth(i)+512);
			}
		
			// 파일 생성
			fileOut = response.getOutputStream(); 
			wb.write(fileOut);
			
			fileOut.close();
		} catch (Exception e) {
			//util.log(MNAME,"에러 발생 : " + e.toString());
		} finally {
			
		}
	}
}
